﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TnaguTetris
{
    /// <summary>
    /// Siia tuleb kõik mis on seotud menüüga.
    /// </summary>
    class Menu
    {
        Mootor mootor;

        public int rgb = 117; // nupu v2rv alguses
        public float alpha = 1; // nupu l2bipaistvus alguses
        public bool fadingIn = false; // kas fadeb sisse v6i v2lja
        public int buttonTime = 0; // nupu vilkumise aeg
        public float menuOffset = 1;
        public enum State
        {
            Mainmenu = 1,
            Pause = 2,
            Gameover = 3,
            Playing = 4,
            Options = 5,
            Credits = 6,
            Multiplayer = 7,
            MultiplayerWaitPlayer = 8,
            MultiplayerJoin = 9,
            MultiplayerReady = 10,
            MultiplayerPlaying = 11,
            Help = 12,
            MultiplayerGameOver = 13
        }
        public static volatile State newMenuState;
        public static volatile State menuState;
        public const int MainmenuIntValue = 1;
        public const int PauseIntValue = 2;
        public const int GameoverIntValue = 3;
        public const int PlayingIntValue = 4;
        public const int OptionsIntValue = 5;
        public const int CreditsIntValue = 6;
        public const int MultiplayerIntValue = 7;
        public const int MultiplayerWaitPlayerIntValue = 8;
        public const int MultiplayerJoinIntValue = 9;
        public const int MultiplayerReadyIntValue = 10;
        public const int MultiplayerPlayingIntValue = 11;
        public const int HelpIntValue = 12;
        public const int MultiplayerGameOverIntValue = 13;
        public bool run = false;

        public String[] menuArray; // menyy nuppude array
        public int menuArraySelected = 0; // milline menyy nupp valitud on
        public int tempMenuArraySelected = 0; // milline menyy nupp valitakse peale animatsiooni
        public Color color; // valitud menyynupu v2rvi deklareerimine
        public int tempSelect = 0; // kas m6ni menyynupp on ajutiselt valitud (optionsis kui saad nooltega vahetada)

        /// <summary>
        /// Klassi konstruktor
        /// </summary>
        public Menu(Mootor mootorO)
        {
            this.mootor = mootorO;
        }
        /// <summary>
        /// Muudab aktiivseks eelmise või järgmise menüü nupu
        /// </summary>
        public void MenuMove(string direction)
        {
            if (menuArray.GetLength(0) != 0)
            {
                if (direction == "down")
                {
                    if (menuArraySelected == menuArray.GetLength(0) - 1)
                    {
                        menuArraySelected = 0;
                    }
                    else
                    {
                        menuArraySelected += 1;
                    }
                    if (menuArray[menuArraySelected] == "")
                    {
                        MenuMove("down");
                    }
                }
                if (direction == "up")
                {
                    if (menuArraySelected == 0)
                    {
                        menuArraySelected = menuArray.GetLength(0) - 1;
                    }
                    else
                    {
                        menuArraySelected -= 1;
                    }
                    if (menuArray[menuArraySelected] == "")
                    {
                        MenuMove("up");
                    }
                }
            }
        }

        /// <summary>
        /// Muudab optionsis muudetud väärtust
        /// </summary>
        public void ChangeOption(string direction)
        {
            switch (tempSelect)
            {
                // alustamise level
                case 1:
                    if (direction == "up")
                    {
                        if (mootor.GetLevel() < 20)
                        {
                            int lvl = mootor.GetLevel();
                            lvl++;
                            mootor.SetLevel(lvl);
                        }
                        else
                        {
                            mootor.SetLevel(0);
                        }
                    }
                    if (direction == "down")
                    {
                        if (mootor.GetLevel() > 0)
                        {
                            int lvl = mootor.GetLevel();
                            lvl--;
                            mootor.SetLevel(lvl);
                        }
                        else
                        {
                            mootor.SetLevel(20);
                        }
                    }
                    break;
                // leveli t6stmise skoor
                case 2:
                    if (direction == "up")
                    {
                        if (mootor.GetScoreLimit() < 50000)
                        {
                            int score = mootor.GetScoreLimit();
                            score += 100;
                            mootor.SetScoreLimit(score);
                        }
                        else
                        {
                            mootor.SetScoreLimit(0);
                        }
                    }
                    if (direction == "down")
                    {
                        if (mootor.GetScoreLimit() > 0)
                        {
                            int score = mootor.GetScoreLimit();
                            score -= 100;
                            mootor.SetScoreLimit(score);
                        }
                        else
                        {
                            mootor.SetScoreLimit(50000);
                        }
                    }
                    break;
                // kas n2idatakse cheatLine
                case 3:
                    if (mootor.GetCheatLine() == 0)
                    {
                        mootor.SetCheatLine(1);
                    }
                    else
                    {
                        mootor.SetCheatLine(0);
                    }
                    break;
                // music
                case 4:
                    if (mootor.GetMusic() == 0)
                    {
                        mootor.SetMusic(1);
                    }
                    else
                    {
                        mootor.SetMusic(0);
                    }
                    break;
                // sfx
                case 5:
                    if (mootor.GetSfx() == 0)
                    {
                        mootor.SetSfx(1);
                    }
                    else
                    {
                        mootor.SetSfx(0);
                    }
                    break;
            }
        }

        /// <summary>
        /// Kuvab mängu menüü. Valitud nupp vilgub.
        /// </summary>
        /// <param name="spriteBatch">Peaklassi joonistaja</param>
        /// <param name="font">Jonistuse font</param>
        public void Draw(SpriteBatch spriteBatch, SpriteFont font)
        {
            if (run)
            {
                if (menuOffset == 0f)
                {
                    menuOffset = 1f;
                }
                menuOffset += menuOffset * 0.18f;
                alpha -= 0.025f;
                if(alpha < 0)
                    alpha = 0f;
                if (menuOffset > Game1.graphics.PreferredBackBufferWidth + 10000)
                {
                    run = false;
                    alpha = 1f;
                    SetState((int)newMenuState, tempMenuArraySelected);
                }
            }
            else
            {
                menuOffset = 0;
            }
            if (menuArray.GetLength(0) != 0)
            {
                for (int i = 0; i < menuArray.GetLength(0); i++)
                {
                    if (menuArray[i] != "")
                    {
                        color = new Color(255, 255, 255)*alpha;
                        if (menuArraySelected == i)
                        {
                            color = new Color(rgb, rgb, 0)*alpha;
                        }
                        spriteBatch.DrawString(font, menuArray[i], new Vector2((int)(2 * menuOffset + 22f), (int)(182 + (i * 25))), new Color(0, 0, 0)*alpha);

                        // kui pole ykski asi options menyys valitud siis on ainult praegune hover
                        if (tempSelect == 0)
                        {
                            spriteBatch.DrawString(font, menuArray[i], new Vector2((int)(2 * menuOffset + 20f), (int)(180 + (i * 25))), color);
                        }
                        // kui on valitud midagi options menyys
                        else
                        {
                            // ja valitud on sama mis i, siis on see heledam kui teised
                            if (tempSelect == i + 1)
                            {
                                spriteBatch.DrawString(font, menuArray[i], new Vector2((int)(2 * menuOffset + 20f), (int)(180 + (i * 25))), color);
                            }
                            // teised on tumedamad
                            else
                            {
                                spriteBatch.DrawString(font, menuArray[i], new Vector2((int)(2 * menuOffset + 20f), (int)(180 + (i * 25))), Color.DarkKhaki*alpha);
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Muutes siin mängu state'i siis toimub ka animatsioon
        /// </summary>
        /// <param name="state">Millisesse state'i liigutakse</param>
        /// <param name="select">Milline menüü nupp on valitud järgmises menüüs</param>
        public void SetNewState(int state, int select)
        {
            newMenuState = (State)state;
            tempMenuArraySelected = select;
            run = true;
        }

        /// <summary>
        /// Tagastab hetke mängu state
        /// </summary>
        /// <returns>hetke state</returns>
        public int GetState()
        {
            return (int)menuState;
        }

        /// <summary>
        /// Muutes siin mängu state'i siis animatsiooni ei toimu ning state ilmub koheselt
        /// </summary>
        /// <param name="state">Millisesse state'i liigutakse</param>
        /// <param name="select">Milline menüü nupp on valitud järgmises menüüs</param>
        public void SetState(int state, int select)
        {
            menuArraySelected = select;
            menuState = (State)state;
        }
    }
}
